﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using DbHelper;
using Models;
using Models.SqlitModel;

namespace DAL
{
    public class BooksDAL
    {
        public SqliteEFHelper db;
        public BooksDAL()
        {
            db = new SqliteEFHelper();
        }
        #region 查询书籍

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="name">书名</param>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public List<Books> QueryBooks(ref DataPage page, string name, string url)
        {
            Expression<Func<Books, bool>> where = PredicateExtensions.True<Books>();

            if (!string.IsNullOrWhiteSpace(name))
                where = where.And(o => o.Name.Contains(name));
            if (!string.IsNullOrWhiteSpace(url))
                where = where.And(o => o.Href.Contains(url));

            page.DataCount = db.FindAll(where).Count();

            var list = db.FindAll<Books>(where)
                .Skip((page.PageIndex - 1) * page.PageSize)
                .Take(page.PageSize);
            return list.ToList();
        }
        /// <summary>
        /// 根据小说名称获取小说列表
        /// </summary>
        /// <param name="names"></param>
        /// <returns></returns>
        public List<Books> QueryBooks(List<string> names)
        {
            var list = db.FindAll<Books>(o => names.Contains(o.Name));
            return list.ToList();
        }
        /// <summary>
        /// 查询未获取章节的小说
        /// </summary>
        /// <returns></returns>
        public Books QueryBook()
        {
            var book = db.FindAll<Books>(o => o.State == "未下载").FirstOrDefault();
            if (book != null)
            {
                book.State = "下载中";
                db.Update(book);
            }
            return book;
        }
        /// <summary>
        /// 查询书籍
        /// </summary>
        /// <param name="name">书名</param>
        /// <returns></returns>
        public Books QueryBookByName(string name)
        {
            var obj = db.Find<Books>(o => o.Name.Contains(name));
            return obj;
        }
        /// <summary>
        /// 查询书籍
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        public Books QueryBookById(string id)
        {
            return db.Find<Books>(id);
        }
        /// <summary>
        /// 获取进度
        /// </summary>
        /// <returns></returns>
        public List<int> QueryProgress()
        {    
            return new List<int>() {
                db.FindAll<Books>(o => o.State == "已下载").Count(),
                db.FindAll<Books>(o => o.State != "已下载").Count()
            };
        }
        #endregion

        #region 增删改
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Books InsertBook(Books obj)
        {
            return db.Insert(obj);
        }
        /// <summary>
        /// 批量新增
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int InsertBook(List<Books> list)
        {
            return db.InsertEntityFields(list);
        }
        /// <summary>
        /// 更新书籍
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public Books UpdateBook(Books obj)
        {
            return db.Update(obj);
        }
        /// <summary>
        /// 批量更新书籍
        /// </summary>
        /// <param name="list"></param>
        /// <param name="fields"></param>
        /// <returns></returns>
        public int UpdateBook(List<Books> list, List<string> fields)
        {
            return db.UpdateEntityFields(list, fields).Count;
        }
        /// <summary>
        /// 物理删除书籍
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int DeleteBook(Books obj)
        {
            return db.Delete(obj);
        }
        /// <summary>
        /// 批量物理删除书籍
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public int DeleteBook(List<Books> list)
        {
            return db.Delete(list);
        }
        #endregion
    }
}
